<?php

namespace app\controller\admin\wz;

use app\model\admin\wz\Wzhero;
use app\model\admin\wz\Wzweapon;
use http\Client;
use laytp\controller\Backend;
use laytp\library\CommonFun;
use think\facade\Db;

/**
 * Class Game
 * @package app\controller\admin\run
 */
class Programme extends Backend
{
    /**
     * api_log模型对象
     * @var \app\model\api\Log
     */
    protected $model;
    protected $hasSoftDel = 0;//是否拥有软删除功能

    protected $noNeedLogin = []; // 无需登录即可请求的方法
    protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法

    public $orderRule = ['id' => 'asc'];

    public function _initialize()
    {
        $this->model = new Wzweapon();
    }

    /**
     * 列表
     *  all_data参数的值为true时，表示查询表中所有数据集，否则进行分页查询
     * @return mixed
     */
    public function index()
    {
        $postData = $this->buildPostParams();
        $data = $this->postData("getHeroProgrammeList", $postData);
        $responseData = $data['data'];
        $responseData['data'] = $responseData['list'];
        unset($responseData['list']);
        return $this->success('数据获取成功', $responseData);
    }


    /**
     * 列表
     *  all_data参数的值为true时，表示查询表中所有数据集，否则进行分页查询
     * @return mixed
     */
    public function list()
    {
        $where = $this->buildSearchParams();
        $order = $this->buildOrder();
        $data = $this->model->where($where)->order($order)->cache(true)->select()->toArray();
        return $this->success('数据获取成功', $data);
    }


    public function info()
    {
        $id = $this->request->param('id');
        $info = $this->postData("readProgramme", ['id' => $id]);
        $info = $info['data'];
        $info['hero_id'] = $info['heroId'];
        $info['label_id'] = $info['labelId'];
        $info['sum_skill_id'] = $info['sumSkillId'];
        $info['skill_priority'] = $info['skillPriority'];
        $info['master_recommend_id'] = $info['masterRecommendId'];
        $info['recommend_reason'] = $info['recommendReason'];
        $info['inscriptions'] = json_decode($info['inscriptions'], true);
        $info['recommendEquipment'] = json_decode($info['recommendEquipment'], true);
        $info['equipmentSort'] = json_decode($info['equipmentSort'], true);
        $info['recommend_equipment1'] = !empty($info['recommendEquipment'][0]) ? $info['recommendEquipment'][0] : '';
        $info['recommend_equipment2'] = !empty($info['recommendEquipment'][1]) ? $info['recommendEquipment'][1] : '';
        $info['recommend_equipment3'] = !empty($info['recommendEquipment'][2]) ? $info['recommendEquipment'][2] : '';
        $info['recommend_equipment4'] = !empty($info['recommendEquipment'][3]) ? $info['recommendEquipment'][3] : '';
        $info['recommend_equipment5'] = !empty($info['recommendEquipment'][4]) ? $info['recommendEquipment'][4] : '';
        $info['recommend_equipment6'] = !empty($info['recommendEquipment'][5]) ? $info['recommendEquipment'][5] : '';

        $info['equipment_sort1'] = !empty($info['equipmentSort'][0]) ? $info['equipmentSort'][0] : '';
        $info['equipment_sort2'] = !empty($info['equipmentSort'][1]) ? $info['equipmentSort'][1] : '';
        $info['equipment_sort3'] = !empty($info['equipmentSort'][2]) ? $info['equipmentSort'][2] : '';
        $info['equipment_sort4'] = !empty($info['equipmentSort'][3]) ? $info['equipmentSort'][3] : '';
        $info['equipment_sort5'] = !empty($info['equipmentSort'][4]) ? $info['equipmentSort'][4] : '';
        $info['equipment_sort6'] = !empty($info['equipmentSort'][5]) ? $info['equipmentSort'][5] : '';
        $info['equipment_sort7'] = !empty($info['equipmentSort'][6]) ? $info['equipmentSort'][6] : '';
        $info['equipment_sort8'] = !empty($info['equipmentSort'][7]) ? $info['equipmentSort'][7] : '';
        $info['equipment_sort9'] = !empty($info['equipmentSort'][8]) ? $info['equipmentSort'][8] : '';
        $info['equipment_sort10'] = !empty($info['equipmentSort'][9]) ? $info['equipmentSort'][9] : '';
        $info['equipment_sort11'] = !empty($info['equipmentSort'][10]) ? $info['equipmentSort'][10] : '';
        $info['equipment_sort12'] = !empty($info['equipmentSort'][11]) ? $info['equipmentSort'][11] : '';

        $info['inscriptions1'] = !empty($info['inscriptions'][0]['id']) ? $info['inscriptions'][0]['id'] : '';
        $info['inscriptions1_num'] = !empty($info['inscriptions'][0]['number']) ? $info['inscriptions'][0]['number'] : '';
        $info['inscriptions2'] = !empty($info['inscriptions'][1]['id']) ? $info['inscriptions'][1]['id'] : '';
        $info['inscriptions2_num'] = !empty($info['inscriptions'][1]['number']) ? $info['inscriptions'][1]['number'] : '';
        $info['inscriptions3'] = !empty($info['inscriptions'][2]['id']) ? $info['inscriptions'][2]['id'] : '';
        $info['inscriptions3_num'] = !empty($info['inscriptions'][2]['number']) ? $info['inscriptions'][2]['number'] : '';
        $info['inscriptions4'] = !empty($info['inscriptions'][3]['id']) ? $info['inscriptions'][3]['id'] : '';
        $info['inscriptions4_num'] = !empty($info['inscriptions'][3]['number']) ? $info['inscriptions'][3]['number'] : '';
        $info['inscriptions5'] = !empty($info['inscriptions'][4]['id']) ? $info['inscriptions'][4]['id'] : '';
        $info['inscriptions5_num'] = !empty($info['inscriptions'][4]['number']) ? $info['inscriptions'][4]['number'] : '';

        if (isset($info->icon)) {
            $info->icon = imgUrlToAbsolute($info->icon);
        }
        if (isset($info->picture)) {
            $info->picture = imgUrlToAbsolute($info->picture);
        }
        return $this->success('获取成功', $info);
    }
//
//    //添加
    public function add()
    {
        $post = CommonFun::filterPostData($this->request->post());
        $equipment_sort = [];
        if (!empty($post['equipment_sort1'])) {
            array_push($equipment_sort, $post['equipment_sort1']);
        }
        if (!empty($post['equipment_sort2'])) {
            array_push($equipment_sort, $post['equipment_sort2']);
        }
        if (!empty($post['equipment_sort3'])) {
            array_push($equipment_sort, $post['equipment_sort3']);
        }
        if (!empty($post['equipment_sort4'])) {
            array_push($equipment_sort, $post['equipment_sort4']);
        }
        if (!empty($post['equipment_sort5'])) {
            array_push($equipment_sort, $post['equipment_sort5']);
        }
        if (!empty($post['equipment_sort6'])) {
            array_push($equipment_sort, $post['equipment_sort6']);
        }
        if (!empty($post['equipment_sort7'])) {
            array_push($equipment_sort, $post['equipment_sort7']);
        }
        if (!empty($post['equipment_sort8'])) {
            array_push($equipment_sort, $post['equipment_sort8']);
        }
        if (!empty($post['equipment_sort9'])) {
            array_push($equipment_sort, $post['equipment_sort9']);
        }
        if (!empty($post['equipment_sort10'])) {
            array_push($equipment_sort, $post['equipment_sort10']);
        }
        if (!empty($post['equipment_sort11'])) {
            array_push($equipment_sort, $post['equipment_sort11']);
        }
        if (!empty($post['equipment_sort12'])) {
            array_push($equipment_sort, $post['equipment_sort12']);
        }
        $inscriptions = [];
        if (!empty($post['inscriptions1'])) {
            array_push($inscriptions, array('id' => $post['inscriptions1'], 'number' => $post['inscriptions1_num']));
        }
        if (!empty($post['inscriptions2'])) {
            array_push($inscriptions, array('id' => $post['inscriptions2'], 'number' => $post['inscriptions2_num']));
        }
        if (!empty($post['inscriptions3'])) {
            array_push($inscriptions, array('id' => $post['inscriptions3'], 'number' => $post['inscriptions3_num']));
        }
        if (!empty($post['inscriptions4'])) {
            array_push($inscriptions, array('id' => $post['inscriptions4'], 'number' => $post['inscriptions4_num']));
        }
        if (!empty($post['inscriptions5'])) {
            array_push($inscriptions, array('id' => $post['inscriptions5'], 'number' => $post['inscriptions5_num']));
        }
        $insData = array(
            'hero_id' => $post['hero_id'],
            'sort' => $post['sort'],
            'label_id' => $post['label_id'],
            'master_recommend_id' => $post['master_recommend_id'],
            'sum_skill_id' => $post['sum_skill_id'],
            'skill_priority' => $post['skill_priority'],
            'recommend_equipment' => json_encode(array($post['recommend_equipment1'], $post['recommend_equipment2'], $post['recommend_equipment3'], $post['recommend_equipment4'], $post['recommend_equipment5'], $post['recommend_equipment6']), JSON_UNESCAPED_UNICODE),
            'equipment_sort' => json_encode($equipment_sort, JSON_UNESCAPED_UNICODE),
            'inscriptions' => json_encode($inscriptions, JSON_UNESCAPED_UNICODE),
            'description' => $post['description'],
            'recommend_reason' => $post['recommend_reason'],
        );
        try {
            $hero = $this->postData("createProgramme", $insData);
            if ($hero) {
                $this->clearCache();
                return $this->success('添加成功', $post);
            } else {
                return $this->error('操作失败');
            }
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }
//
//    //编辑
    public function edit()
    {
        $id = $this->request->param('id');
        $info = $this->postData("readProgramme", ['id' => $id]);
        if (!$info) {
            return $this->error('ID参数错误');
        }
        $post = CommonFun::filterPostData($this->request->post());
        $equipment_sort=[];
        if (!empty($post['equipment_sort1'])){
            array_push($equipment_sort,$post['equipment_sort1']);
        }
        if (!empty($post['equipment_sort2'])){
            array_push($equipment_sort,$post['equipment_sort2']);
        }
        if (!empty($post['equipment_sort3'])){
            array_push($equipment_sort,$post['equipment_sort3']);
        }
        if (!empty($post['equipment_sort4'])){
            array_push($equipment_sort,$post['equipment_sort4']);
        }
        if (!empty($post['equipment_sort5'])){
            array_push($equipment_sort,$post['equipment_sort5']);
        }
        if (!empty($post['equipment_sort6'])){
            array_push($equipment_sort,$post['equipment_sort6']);
        }
        if (!empty($post['equipment_sort7'])){
            array_push($equipment_sort,$post['equipment_sort7']);
        }
        if (!empty($post['equipment_sort8'])){
            array_push($equipment_sort,$post['equipment_sort8']);
        }
        if (!empty($post['equipment_sort9'])){
            array_push($equipment_sort,$post['equipment_sort9']);
        }
        if (!empty($post['equipment_sort10'])){
            array_push($equipment_sort,$post['equipment_sort10']);
        }
        if (!empty($post['equipment_sort11'])){
            array_push($equipment_sort,$post['equipment_sort11']);
        }
        if (!empty($post['equipment_sort12'])){
            array_push($equipment_sort,$post['equipment_sort12']);
        }
        $inscriptions=[];
        if (!empty($post['inscriptions1'])){
            array_push($inscriptions,array('id'=>$post['inscriptions1'],'number'=>$post['inscriptions1_num']));
        }
        if (!empty($post['inscriptions2'])){
            array_push($inscriptions,array('id'=>$post['inscriptions2'],'number'=>$post['inscriptions2_num']));
        }
        if (!empty($post['inscriptions3'])){
            array_push($inscriptions,array('id'=>$post['inscriptions3'],'number'=>$post['inscriptions3_num']));
        }
        if (!empty($post['inscriptions4'])){
            array_push($inscriptions,array('id'=>$post['inscriptions4'],'number'=>$post['inscriptions4_num']));
        }
        if (!empty($post['inscriptions5'])){
            array_push($inscriptions,array('id'=>$post['inscriptions5'],'number'=>$post['inscriptions5_num']));
        }
        $data=array(
            'id'=>$post['id'],
            'hero_id'=>$post['hero_id'],
            'sort'=>$post['sort'],
            'skill_priority'=>$post['skill_priority'],
            'label_id'=>$post['label_id'],
            'description'=>$post['description'],
            'sum_skill_id'=>$post['sum_skill_id'],
            'recommend_equipment'=>json_encode(array($post['recommend_equipment1'],$post['recommend_equipment2'],$post['recommend_equipment3'],$post['recommend_equipment4'],$post['recommend_equipment5'],$post['recommend_equipment6']),JSON_UNESCAPED_UNICODE),
            'master_recommend_id'=>$post['master_recommend_id'],
            'recommend_reason'=>$post['recommend_reason'],
            'equipment_sort'=>json_encode($equipment_sort,JSON_UNESCAPED_UNICODE),
            'inscriptions'=>json_encode($inscriptions,JSON_UNESCAPED_UNICODE),
        );

        try {
            $response = $this->postData("updateProgramme",$data);
            if ($response['code'] == 0) {
                $this->clearCache();
                return $this->success('添加成功', $post);
            } else {
                return $this->error($response['message']);
            }
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }
//
//    //删除
    public function del()
    {
        $ids = array_filter($this->request->post('ids'));
        if (!$ids) {
            return $this->error('参数ids不能为空');
        }
        try{
            $response = $this->postData("deleteProgramme",[
                'id' => $ids[0],
            ]);
            if ($response['code'] == 0) {
                $this->clearCache();
                return $this->success('删除成功');
            } else {
                return $this->error($response['message']);
            }
        }catch (\Exception $e){
            return $this->exceptionError($e);
        }
    }

}